Fancy Tables
Exploring different methods of creating visually satisfying data tables. Admit it, we do not like the tradition excel format: too ugly to understand.
Messing Around With Data Tables
DT Table Code
ds_starter <- ds %>%
mutate(province = as.factor(province),
price = price,
thetaPointMean = mean(points),
thetaPriceMean = mean(price))
ds_starter %>%
arrange(province, year) %>%
select(Province = province,
Year = year,
Price = price,
Points = points,
Description = description) %>%
datatable(.,
filter = "bottom",
extensions = 'Buttons',
options = list(dom = 'Bfrtip',
buttons = c('copy', 'csv', 'excel'),
initComplete = JS("function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#131F4F', 'color': '#fff'});",
"}")))Stepping Up The Table Game
GT Table Code
fancyTbl <- ds_summary %>%
gt() %>%
# format the numeric output to 3 digit rounding
fmt_number(columns = c(pointsMean, pointsSD, priceMean, priceSD),
decimals = 3) %>%
# create nice labels for a few ugly variable names
cols_label(province = "Province",
pointsMean = "Avg. Points",
pointsSD = "Std. Dev. Points",
priceMean = "Avg. Price",
priceSD = "Std. Dev. Price",
points = "Points Trend",
price = "Price Trend",) %>%
# Plot the sparklines from the list column
gt_plt_sparkline(points,
type="ref_median",
same_limit = TRUE
) %>%
gt_plt_sparkline(price,
type="ref_median",
same_limit = TRUE
) %>%
# use the guardian's table theme
gt_theme_guardian() %>%
# give hulk coloring to the Mean Human Rights Score
gt_hulk_col_numeric(pointsMean) %>%
gt_hulk_col_numeric(priceMean) %>%
# create a header and subheader
tab_header(title="Province Pinot Wine Summary", subtitle = "Source: Dr. Hendrick") %>%
# attach excel file
tab_source_note(excel_file_attachment)
# save the original as an image
#gtsave(fancyTbl, "table.png")
# show the table themed in accordance with the page
fancyTbl